<route lang="jsonc">{
  "style": {
    "navigationBarTitleText": "银行卡"
  }
}</route>

<script lang="ts" setup>
import { onLoad } from '@dcloudio/uni-app'
import { reactive } from 'vue'
import { bankInfo, viewBankInfo } from '@/api/user'

const form = reactive({
  bank_no: '',
  bank_user: '',
  bank_name: '',
  bank_addr: '',
  bank_branch: '',
})

onLoad(async () => {
  const res = await viewBankInfo()
  if (res.length) {
    Object.assign(form, res[res.length - 1])
  }
  console.log(form, '8888')
})

async function handleSubmit() {
  const requiredFields: { key: keyof typeof form, label: string }[] = [
    { key: 'bank_no', label: '银行卡号' },
    { key: 'bank_user', label: '持卡人姓名' },
    { key: 'bank_name', label: '开户银行' },
    { key: 'bank_addr', label: '开户银行地区' },
    { key: 'bank_branch', label: '支行名称' },
  ]

  for (const field of requiredFields) {
    if (!form[field.key]) {
      uni.showToast({
        title: `请输入${field.label}`,
        icon: 'none',
      })
      return
    }
  }
  await bankInfo(form)
  uni.showToast({
    title: '保存成功',
    icon: 'success',
  })
  uni.navigateBack()
}
</script>

<template>
  <view class="container">
    <view class="form-wrapper">
      <view class="form-item">
        <view class="label">
          银行卡号
        </view>
        <view class="input-wrapper">
          <wd-input v-model="form.bank_no" type="number" placeholder-style="color: #c0c4cc" no-border
            placeholder="请输入您的银行卡号" />
        </view>
      </view>
      <view class="form-item">
        <view class="label">
          持卡人姓名
        </view>
        <view class="input-wrapper">
          <wd-input v-model="form.bank_user" placeholder-style="color: #c0c4cc" no-border placeholder="请输入持卡人姓名" />
        </view>
      </view>
      <view class="form-item">
        <view class="label">
          开户银行
        </view>
        <view class="input-wrapper">
          <wd-input v-model="form.bank_name" placeholder-style="color: #c0c4cc" no-border placeholder="请输入开户银行" />
        </view>
      </view>
      <view class="form-item">
        <view class="label">
          开户银行地区
        </view>
        <view class="input-wrapper">
          <wd-input v-model="form.bank_addr" placeholder-style="color: #c0c4cc" no-border placeholder="请输入开户银行地区" />
        </view>
      </view>
      <view class="form-item">
        <view class="label">
          支行名称
        </view>
        <view class="input-wrapper">
          <wd-input v-model="form.bank_branch" placeholder-style="color: #c0c4cc" no-border placeholder="请输入支行名称" />
        </view>
      </view>
    </view>

    <view class="submit-button-wrapper">
      <wd-button block custom-class="submit-button" @click="handleSubmit">
        提交
      </wd-button>
    </view>
  </view>
</template>

<style lang="scss" scoped>
.container {
  background-color: #fcf8f1;
  min-height: 100vh;
  padding: 1rem;
}

.form-wrapper {
  background-color: white;
  padding: 1rem;
  border-radius: 8px;
}

.form-item {
  margin-bottom: 1rem;

  .label {
    margin-bottom: 0.5rem;
    font-size: 0.875rem;
    color: #333;
  }

  .input-wrapper {
    border: 1px dashed #e0e0e0;
    border-radius: 4px;
    padding: 0 0.75rem;
    height: 44px;
    display: flex;
    align-items: center;
  }

  .picker-value {
    font-size: 14px;
    width: 100%;

    &.placeholder {
      color: #c0c4cc;
    }
  }
}

.submit-button-wrapper {
  margin-top: 2rem;
}

:deep(.submit-button) {
  background-color: #ff770d !important;
  border-color: #ff770d !important;
  color: #ffffff !important;
  font-weight: bold;
  font-size: 30rpx !important;
  border-radius: 9999px !important;
  height: 44px !important;
}

:deep(.wd-input) {
  width: 100%;
  --wd-input-font-size: 14px;
}
</style>
